package service

import (
	"admin-api/app/dao"
	"admin-api/app/models/entity"
	"admin-api/app/models/request"
	"admin-api/app/models/response"
	"admin-api/core"
	"admin-api/utils"
	"gitee.com/molonglove/goboot/gorm"
	"time"
)

var Config = new(ConfigService)

type ConfigService struct{}

// Create 参数创建
func (d *ConfigService) Create(param *request.ConfigCreateRequest) *response.BusinessError {
	var (
		dict  entity.Setting
		now   time.Time
		err   error
		exist bool
	)
	// 检测参数名称是否唯一
	if exist = dao.Config.ExistSameConfig(param.ConfigName, "", 0); err != nil || exist {
		core.Log.Error("存在相同的岗位名称")
		return response.CustomBusinessError(response.Failed, "存在相同的参数名称")
	}
	if exist = dao.Config.ExistSameConfig("", param.ConfigKey, 0); err != nil || exist {
		core.Log.Error("存在相同的岗位编码")
		return response.CustomBusinessError(response.Failed, "存在相同的参数编码")
	}
	// 保存菜单数据
	now = time.Now()
	dict = entity.Setting{
		ConfigName:  param.ConfigName,
		ConfigKey:   param.ConfigKey,
		ConfigValue: param.ConfigValue,
		ConfigType:  param.ConfigType,
		CreateBy:    param.UserName,
		Remark:      param.Remark,
		CreateTime:  &now,
	}
	if err = core.DB.Create(&dict).Error; err != nil {
		core.Log.Error("创建参数配置失败: %s", err.Error())
		return response.CustomBusinessError(response.Failed, "创建参数配置失败")
	}
	core.Log.Info("创建参数[%d:%s]成功", dict.ConfigId, dict.ConfigName)
	return nil
}

// Update 参数更新
func (d *ConfigService) Update(param *request.ConfigUpdateRequest) *response.BusinessError {
	var (
		old   entity.Setting
		now   time.Time
		err   error
		exist bool
	)
	if err = core.DB.Builder().
		Select().
		From("sys_setting").
		Where(gorm.Eq("config_id", param.ConfigId)).
		QExecute(&old).
		Error; err != nil {
		core.Log.Error("当前参数不存在：%s", err.Error())
		return response.CustomBusinessError(response.Failed, "当前参数不存在")
	}
	// 检测岗位名称是否唯一
	if exist = dao.Config.ExistSameConfig(param.ConfigName, "", param.ConfigId); err != nil || exist {
		core.Log.Error("存在相同的岗位名称")
		return response.CustomBusinessError(response.Failed, "存在相同的参数名称")
	}
	if exist = dao.Config.ExistSameConfig("", param.ConfigKey, param.ConfigId); err != nil || exist {
		core.Log.Error("存在相同的岗位编码")
		return response.CustomBusinessError(response.Failed, "存在相同的参数编码")
	}
	// 保存部门数据
	now = time.Now()
	old.ConfigName = param.ConfigName
	old.ConfigKey = param.ConfigKey
	old.ConfigValue = param.ConfigValue
	old.ConfigType = param.ConfigType
	old.Remark = param.Remark
	old.UpdateBy = param.UserName
	old.UpdateTime = &now
	if err = core.DB.Save(&old).Error; err != nil {
		core.Log.Error("更新参数[%d]:%s", param.ConfigId, err.Error())
		return response.CustomBusinessError(response.Failed, "更新参数失败")
	}
	return nil
}

// Info 参数详情
func (d *ConfigService) Info(configId int64) (*response.ConfigInfoResponse, *response.BusinessError) {
	var (
		result response.ConfigInfoResponse
		config entity.Setting
		err    error
	)
	if err = core.DB.Builder().
		Select().
		From("sys_config").
		Where(gorm.Eq("config_id", configId)).
		QExecute(&config).
		Error; err != nil {
		return nil, response.CustomBusinessError(response.Failed, "当前参数不存在")
	}
	_ = utils.StructCopy(config, &result)
	return &result, nil
}

// Page 参数分页
func (d *ConfigService) Page(param *request.ConfigPageRequest) (*response.PageData, *response.BusinessError) {
	var (
		list  []response.ConfigTableResponse
		total int64
		err   error
	)
	if err = core.DB.Namespace("config").
		DQuery("selectConfigPage", param).
		DOffset(param.Page, param.Size).
		TPage(&list, &total).
		Error; err != nil {
		core.Log.Error("统计参数数据失败, 异常信息如下：%s", err.Error())
		return nil, response.CustomBusinessError(response.Failed, "获取参数数据失败")
	}
	return &response.PageData{
		Total: total,
		Page:  param.Page,
		Size:  param.Size,
		Data:  list,
	}, nil
}

// Delete 参数删除
func (d *ConfigService) Delete(param *request.ConfigDeleteRequest, username string) *response.BusinessError {
	if err := core.DB.Builder().
		Delete().
		From("sys_config").
		Where(gorm.In("config_id", param.Ids)).
		DExecute().
		Error; err != nil {
		core.Log.Error("删除参数失败：%s", err.Error())
		return response.CustomBusinessError(response.Failed, "删除参数失败")
	}
	return nil
}
